package JHfstools;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.AbstractAction;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.Document;
import javax.swing.undo.UndoManager;
import javax.swing.undo.UndoableEdit;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.JMenuBar;
import javax.swing.JTextPane;
import java.awt.Color;

public class redotest extends JFrame {

	private JPanel contentPane;
	protected UndoAct undoAction = new UndoAct();
	protected RedoAct redoAction = new RedoAct();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					redotest frame = new redotest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	UndoManager undo = new UndoManager();
	/**
	 * Create the frame.
	 */
	public redotest() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 686, 604);
		JMenuBar menuBar = new JMenuBar();
		//menuBar.setBackground(Color.CYAN);
		//setJMenuBar(menuBar);
		
		menuBar.setPreferredSize(new Dimension(400,15));
		menuBar.setLocation(5, 5);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		JTextPane textPane = new JTextPane();
		textPane.setPreferredSize(new Dimension(399, 480));
		textPane.setLocation(25, 75);
		textPane.getDocument().addUndoableEditListener(undoAction);
		textPane.getDocument().addUndoableEditListener(redoAction);
		contentPane.add(textPane, BorderLayout.SOUTH);
		JMenu editMenu = new JMenu("Edit");
		editMenu.add(new JMenuItem(undoAction));
		editMenu.add(new JMenuItem(redoAction));
		menuBar.add(editMenu);
		contentPane.add(menuBar);

	}
public class UndoAct extends AbstractAction implements UndoableEditListener{
	private UndoableEdit edit;
	public UndoAct() {
		super("Undo");
		setEnabled(false);
	}
	public void updateEnabled(){
		setEnabled(edit.canUndo());
	}
	public void undoableEditHappened(UndoableEditEvent event){
		edit = event.getEdit();
		putValue(NAME,edit.getUndoPresentationName());
		updateEnabled();
	}
	public void actionPerformed(ActionEvent ae){
		edit.undo();
		updateEnabled();
		redoAction.updateEnabled();
	}
}
public class RedoAct extends AbstractAction implements UndoableEditListener{
	private UndoableEdit edit;
	public RedoAct(){
		super("Redo");
		setEnabled(false);
	}
	public void updateEnabled(){
		setEnabled(edit.canRedo());
	}
	public void undoableEditHappened(UndoableEditEvent event){
		edit = event.getEdit();
		putValue(NAME,edit.getRedoPresentationName());
		updateEnabled();
	}
	public void actionPerformed(ActionEvent ae){
		edit.redo();
		updateEnabled();
		undoAction.updateEnabled();
	}
}

}
